// ignore_for_file: unused_element

import 'package:enterprise_v6_app/app/core/core.dart';

abstract class TreeChoiceIface {
  List<TreeChoiceIface> get treeChildren;
  String get treeId;
  String get treeName;
  bool get expand;
  set expand(bool value);

  @override
  String toString() {
    return 'TreeChoiceIface{id: $treeId, name: $treeName}';
  }
}

class SimpleTreeChoiceIface extends TreeChoiceIface {
  SimpleTreeChoiceIface({
    required this.treeName,
    String? id,
    this.treeChildren = const [],
    this.expand = false,
  }) : treeId = id ?? treeName;

  @override
  final String treeId;

  @override
  final String treeName;

  @override
  final List<TreeChoiceIface> treeChildren;

  @override
  bool expand;
}

final simple = <SimpleTreeChoiceIface>[
  SimpleTreeChoiceIface(
    treeName: '1',
    treeChildren: [
      SimpleTreeChoiceIface(
        treeName: '1-1',
        treeChildren: [
          SimpleTreeChoiceIface(
            treeName: '1-1-1',
          ),
          SimpleTreeChoiceIface(
            treeName: '1-1-2',
          ),
        ],
      ),
      SimpleTreeChoiceIface(
        treeName: '1-2',
      ),
    ],
  ),
  SimpleTreeChoiceIface(
    treeName: '2',
  ),
];

class CustomTreeBuilder extends StatelessWidget {
  const CustomTreeBuilder({
    required this.data,
    this.onSelected,
    this.leadingBuilder,
    this.borderBuilder,
    this.backgroundColorBuilder,
    this.onExpandChanged,
    super.key,
  });

  final List<TreeChoiceIface> data;
  final void Function(TreeChoiceIface)? onSelected;
  final Widget? Function(TreeChoiceIface)? leadingBuilder;
  final Border? Function(TreeChoiceIface)? borderBuilder;
  final Color? Function(TreeChoiceIface)? backgroundColorBuilder;
  final void Function(TreeChoiceIface, bool)? onExpandChanged;

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      padding: const EdgeInsets.symmetric(
        vertical: 10,
        horizontal: 10,
      ),
      itemBuilder: (context, index) {
        return _buildItem(data[index]).paddingSymmetric(vertical: 5);
      },
      itemCount: data.length,
    );
  }

  Widget _buildItem(TreeChoiceIface item) {
    late final Widget child;
    if (item.treeChildren.isEmpty) {
      child = ListTile(
        leading: leadingBuilder?.call(item),
        title: Text(item.treeName, style: TextStyle(fontSize: 13.w)),
        contentPadding: const EdgeInsets.only(left: 17),
        onTap: () => onSelected?.call(item),
        shape: borderBuilder?.call(item) ?? _shape,
        tileColor: backgroundColorBuilder?.call(item),
      );
    } else {
      child = _ExpandTile(
        leading: leadingBuilder?.call(item),
        title: Text(item.treeName, style: TextStyle(fontSize: 13.w)),
        initiallyExpanded: item.expand,
        childrenPadding: const EdgeInsets.only(left: 17),
        shape: borderBuilder?.call(item) ?? _shape,
        tilePadding: const EdgeInsets.only(left: 17, right: 13),
        onExpansionChanged: (value) => onExpandChanged?.call(item, value),
        children: item.treeChildren.map(_buildItem).toList(),
      );
    }
    return Container(
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(13),
        color: Colors.white,
      ),
      child: child,
    );
  }
}

const _shape = Border();

class _ExpandTile extends ExpansionTile {
  const _ExpandTile({
    required super.title,
    super.key,
    super.leading,
    super.children,
    super.subtitle,
    super.initiallyExpanded,
    super.tilePadding,
    super.childrenPadding,
    super.trailing,
    super.controller,
    super.shape,
    super.backgroundColor,
    super.onExpansionChanged,
  }) : super(collapsedShape: _shape);
}
